package database.com;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

public class DatabaseHelper extends SQLiteOpenHelper {

	private static final int VERSION = 1;
	
	public DatabaseHelper(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
		// TODO Auto-generated constructor stub
	}
	
	public DatabaseHelper(Context context, String name) {
		this(context, name, VERSION);
		// TODO Auto-generated constructor stub
	}
	
	public DatabaseHelper(Context context, String name,
			int version) {
		this(context, name, null, version);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		db.execSQL("create table video(_id integer primary key autoincrement,video_id integer,video_title text,video_name text,video_size integer, video_playUrl text, video_downloadUrl text, video_time text, video_info text, video_sort text, image_url text, image_name text, image_size integer, video_localUrl text, image_localUrl text);");
		db.execSQL("create table season(_id integer primary key autoincrement, season_id integer, season_title text, season_count integer, season_info text, pic_url text, pic_name text, pic_size integer, image_url text, image_name text, image_size integer, pic_localUrl text, image_localUrl text, season_sort text)");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub

	}
	
	public Cursor query(String table, String keyName, String where, String value, String[] args)
	{
		String sql = null;
		if(where == null){
			sql = " select " + keyName + " from " + table + ";";
		}
		else{
			sql = " select " + keyName + " from " + table + " where " + where + " = '" + value + "';";
		}
		SQLiteDatabase db = this.getReadableDatabase();
		return db.rawQuery(sql, args);
	}
	
	public int getCount(String table)
	{
		String sql = null;
		sql = " select count(*) " + " from " + table + ";";
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.rawQuery(sql, null);
		cursor.moveToFirst();
		int count = cursor.getInt(0);
		return count;
	}
}
